<?php
/**
 * @title 147. 对链表进行插入排序 - 逆向迭代
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function insertionSortList($head) {
        /**
         * @since 2020-11-24 栈
         */
        $root = $head;
        $stacks = [$root];

        /**
         * @since 2020-11-24 迭代
         */
        $node = $root->next;
        while($node !== null){
            $val = $node->val;

            $list = [];
            $n=1;
            while(true){
                $p = array_pop($stacks);
                if($p === null){
                    /**
                     * @since 2020-11-24 队列长度
                     */
                    $len = count($list);

                    /**
                     * @since 2020-11-24 node插入头部
                     */
                    $nodeNext = $node->next;
                    $list[0]->next = $nodeNext;
                    $node->next = $list[$len-1];
                    $head = $node;

                    /**
                     * @since 2020-11-24 list反转成新的栈
                     * @since 2020-11-24 下次迭代
                     */
                    $list[] = $node;
                    $node = $nodeNext;
                    $stacks = array_reverse($list);
                    break;
                } else {
                    if($p->val <= $val){
                        /**
                         * @since 2020-11-24 找到空位
                         */
                        if(empty($list)){
                            /**
                             * @since 2020-11-24 未弹出元素, 停留当前位置
                             */
                            $stacks[] = $p;
                            $stacks[] = $node;
                            $node = $node->next;
                        } else {
                            /**
                             * @since 2020-11-24 队列长度
                             */
                            $len = count($list);

                            /**
                             * @since 2020-11-24 插入
                             */
                            $nodeNext = $node->next;
                            $p->next = $node;
                            $node->next = $list[$len-1];
                            $list[0]->next = $nodeNext;

                            /**
                             * @since 2020-11-24 list插回去
                             */
                            $stacks[] = $p;
                            $stacks[] = $node;
                            for ($i=$len-1; $i>=0; $i--){
                                $stacks[] = $list[$i];
                            }

                            /**
                             * @since 2020-11-24 下次迭代位置
                             */
                            $node = $nodeNext;
                        }
                        
                        break;
                    } else {
                        /**
                         * @since 2020-11-24 放入list
                         */
                        $list[] = $p;
                    }
                }
            }
        }

        /**
         * @return
         */
        return $head;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202011/2020.11.20_2.php
 */
$datas = [
    [4,2,1,3],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    // $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    $result = $obj->insertionSortList($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
